package com.ns.school.service.edu.impl;

import com.ns.school.common.config.SecurityUtil;
import com.ns.school.po.edu.BorrowBookDB;
import com.ns.school.po.edu.LayuiResult;
import com.ns.school.mapper.edu.BorrowBookMapper;
import com.ns.school.service.edu.BorrowBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class BorrowBookServiceImpl implements BorrowBookService {
    @Autowired
    private BorrowBookMapper borrowBookMapper;


    @Override
    public List<BorrowBookDB> findBorrowBook(BorrowBookDB borrowBookDB) {
        return borrowBookMapper.findBorrowBook(borrowBookDB);
    }


    @Override
    public int checkBorrow(BorrowBookDB borrowBookDB) {
        return borrowBookMapper.checkBorrow(borrowBookDB);
    }

    @Override
    public BorrowBookDB findBorrowById(Integer id) {
        return borrowBookMapper.findBorrowById(id);
    }

    @Override
    public BorrowBookDB findReviewById(Integer id) {
        return borrowBookMapper.findReviewById(id);
    }

    @Override
    public int revertBook(BorrowBookDB borrowBookDB) {
        return borrowBookMapper.revertBook(borrowBookDB);
    }

    //借阅
    @Transactional
    public void borrowBook(LayuiResult result,BorrowBookDB borrowBookDB){
        Integer userId = SecurityUtil.id();
        borrowBookDB.setCreateBy(userId);
        int count =borrowBookMapper.borrowBook(borrowBookDB);
        if (count > 0) {
            result.setCode(200);
            result.setMsg("借阅成功");
        }
    }


    //审核通过
    @Transactional
    public void auditBorrow(LayuiResult result,BorrowBookDB borrowBookDB) {

        // 获取当前日期和时间
        Date currentDate = new Date();
//        // 定义日期时间格式
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        // 格式化日期时间
//        String formattedDate = dateFormat.format(currentDate);
        borrowBookDB.setReviewTime(currentDate);
        int audited =borrowBookMapper.auditBorrowTrue(borrowBookDB);
        if (audited == 1) {
            BorrowBookDB p =borrowBookMapper.findBorrowById(borrowBookDB.getBorrowId());
            borrowBookMapper.auditBorrow(p);
            result.setCode(0);
            result.setMsg("审核成功！");
        } else {
            result.setCode(500);
            result.setMsg("审核失败");
        }

    }

    @Override
    public BorrowBookDB editBorrowBook(Integer id) {
        return borrowBookMapper.editBorrowBook(id);
    }


    //审核拒绝
    @Transactional
    public void auditFailBooks(LayuiResult result,BorrowBookDB borrowBookDB) {

        // 获取当前日期和时间
        Date currentDate = new Date();
        // 定义日期时间格式
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        // 格式化日期时间
//        String formattedDate = dateFormat.format(currentDate);
        borrowBookDB.setReviewTime(currentDate);
        int audited = borrowBookMapper.auditBorrowFail(borrowBookDB);
        if (audited == 1) {
            result.setCode(0);
            result.setMsg("审核拒绝！");
        } else {
            result.setCode(500);
            result.setMsg("审核失败");
        }
    }





}
